import type { StatefulSet } from "../../../api/k8s/types";

import { useK8SApi } from "../../../api/k8s/k8s";
import { SourceMan } from "../providers/sourceCtl";
import type { Ref } from "vue";
import type { StatefulSetQuery } from "@/views/domain/kubeHud/api/k8s/statefulset";
const { statefulSetRepo } = useK8SApi();

class StatefulSetProvider extends SourceMan<StatefulSet> {
  get statefulSetList(): Ref<StatefulSet[]> {
    return this.srcList;
  }

  // 获取节点列表
  getStatefulSets = async (req?: StatefulSetQuery) => {
    const res = await statefulSetRepo.getStatefulSet({
      ...(req || this.searchQuery.value),
      continue: this.continue,
      limit: this.pager.limit.value
    });
    if (res.ok) {
      this.continue = res.data.metadata.continue;
      this.srcList.value = res.data.items;
      this.pager.total.value =
        res.data.metadata.remainingItemCount ?? res.data.items.length;
    }
  };

  // 获取节点详情
  getStatefulSetDetails = async (query: { ns?: string; name?: string }) => {
    const res = await statefulSetRepo.getStatefulSetDetail(query);
    if (res.ok) {
      this.srcDetail.value = res.data;
      this.srcYamlContent.value = this.objectToYaml(res.data);
      this.yamlDialog.value = true;
    }
  };

  // 更新节点
  updateStatefulSet = async (data?: { namespace?: string; name?: string }) => {
    const res = await statefulSetRepo.updateStatefulSet(data);
    if (res.ok) {
      this.refreshStatefulSetList();
    }
  };

  // delete
  deleteStatefulSet = async (query?: { namespace?: string; name?: string }) => {
    const res = await statefulSetRepo.deleteStatefulSet(query);
    if (res.ok) {
      this.refreshStatefulSetList();
    }
  };

  refreshStatefulSetList = async () => {
    this.continue = undefined;
    await this.getStatefulSets({});
  };
}

export default new StatefulSetProvider();
